package com.jsz.peini.common.util;

import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Properties;
import java.util.Random;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CommonUtils {
	private static final Logger logger = LoggerFactory.getLogger(CommonUtils.class);
	//读取配置文件
	public String getValue(String key)throws IOException {  
        Properties props = new Properties();  
        InputStream in = null;  
        try {  
        	in = this.getClass().getResourceAsStream("/config.properties");  
            props.load(in);  
            String value = props.getProperty(key);  
            // 有乱码时要进行重新编码  
            // new String(props.getProperty("name").getBytes("ISO-8859-1"), "GBK");  
            return value;  
  
        } catch (IOException e) {  
            logger.error(e.getMessage(),e);  
            return null;  
  
        } finally {  
            if (null != in)  
                in.close();  
        }  
    }
	
	/** 
	  * 创建指定数量的随机字符串 
	  * @param numberFlag 是否是数字 
	  * @param length 
	  * @return 
	  */  
	 public static String createRandom(boolean numberFlag, int length){  
		  String retStr = "";  
		  String strTable = numberFlag ? "1234567890" : "1234567890abcdefghijkmnpqrstuvwxyz";  
		  int len = strTable.length();  
		  boolean bDone = true;  
		  do {  
		   retStr = "";  
		   int count = 0;  
		   for (int i = 0; i < length; i++) {  
		    double dblR = Math.random() * len;  
		    int intR = (int) Math.floor(dblR);  
		    char c = strTable.charAt(intR);  
		    if (('0' <= c) && (c <= '9')) {  
		     count++;  
		    }  
		    retStr += strTable.charAt(intR);  
		   }  
		   if (count >= 2) {  
		    bDone = false;  
		   }  
		  } while (bDone);  
		  
		  return retStr;  
	 }
	 
	 /**   
	 * 通过经纬度获取距离(单位：米)   
	 * @param xpointMe  x 
	 * @param ypointMe  y
	 * @param xpointHe  
	 * @param ypointHe   
	 * @return   
	 */     
	private static final double EARTH_RADIUS = 6378.137;    
	private static double rad(double d) {    
		return d * Math.PI / 180.0;    
	}    
	
	public static int contrastMap(String xpointMe,String ypointMe,String xpointHe,String ypointHe){
		return contrastMap(Double.parseDouble(xpointMe), Double.parseDouble(ypointMe),Double.parseDouble(xpointHe),Double.parseDouble(ypointHe));
	}
	
	public static int contrastMap(Double xpointMe,Double ypointMe,Double xpointHe,Double ypointHe){
		if(xpointMe == null || ypointMe==null || xpointHe==null || ypointHe==null){
			return 0;
		}
		double radLat1 = rad(xpointMe);    
		double radLat2 = rad(xpointHe);   
		double a = radLat1 - radLat2;    
		double b = rad(ypointMe) - rad(ypointHe);    
		double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)    
				+ Math.cos(radLat1) * Math.cos(radLat2)    
				* Math.pow(Math.sin(b / 2), 2)));    
		s = s *  EARTH_RADIUS;    
		s = Math.round(s * 10000d) / 10000d;    
		s = s*1000;    
		int result = (int)s;
		return result;
	}
	
	//获取当前日期星期阿拉伯数字
	public static int getWeekNum(){
		int weekNum = 0;
		Calendar c = Calendar.getInstance();
		weekNum = c.get(Calendar.DAY_OF_WEEK)-1;
		if(weekNum == 0){
			weekNum = 7;
		}
		return weekNum;
	}
	
	public static int getWeekNum(Date date){
		int weekNum = 0;
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		weekNum = c.get(Calendar.DAY_OF_WEEK)-1;
		if(weekNum == 0){
			weekNum = 7;
		}
		return weekNum;
	}
	
	//double过滤.0
	public static String doubleTrans(double num){
	    if(num % 1.0 == 0){
		        return String.valueOf((long)num);
	    }
	    return String.valueOf(num);
	}
	
	//获取随机昵称
	public static String getRandom(){
		String d=new Random().nextInt(9)+new SimpleDateFormat("yyMMddHHmmssSS").format(Calendar.getInstance().getTime())+new Random().nextInt(9);
		return d;
	}
	 
}
